//按钮点击事件
import { ajax, toast, print, pageTo, output } from 'nc-lightapp-front';
import config from '../config';
import _ from 'lodash-es';

export default function (props, key) {
    const tradeType = this.state.tradeType;

    if (key === 'add') {
        pageTo.specialOpenTo(
            `${config.pull.url}tradetype=${tradeType}&toappcode=${config.appcode}&topagecode=${config.pull.info.topagecode}`,
            {
                appcode: config.pull.info.appcode,
                pagecode: config.pull.info.pagecode,
                status: 'add'
            }
        );
        return;
    } else if (key === 'add_manual') {
        pageTo.specialOpenTo(config.add.url, {
            appcode: config.add.appcode,
            pagecode: config.add.pagecode,
            status: 'add'
        });
        return;
    } else if (key === 'add_refer') {
        pageTo.specialOpenTo(
            `${config.pull.url}tradetype=${tradeType}&toappcode=${config.add.appcode}&topagecode=${config.add.pagecode}`,
            {
                appcode: config.pull.info.appcode,
                pagecode: config.pull.info.pagecode,
                status: 'add'
            }
        );
        return;
    }

    const checkedRows = props.table.getCheckedRows(config.bodycode);
    const record = _.get(checkedRows, '[0].data.values', {});
    const billType = _.get(record, 'pk_billtype.value', '');
    if (checkedRows.length === 0) {
        toast({
            color: 'warning',
            content: this.state.multiLang['select_one_row']
        });
        return;
    }

    //打印
    if (key === 'print') {
        const bills = [];
        checkedRows.forEach(item => {
            bills.push({
                billId: _.get(item, 'data.values.pk_jkbx.value', ''),
                billType: tradeType
            });
        });
        ajax({
            url: '/nccloud/erm/pub/ErmBatchPrintValidaAction.do',
            data: {
                billdata: bills
            },
            success: res => {
                if (!_.get(res, 'data.canPrint', false)) {
                    toast({
                        color: 'error',
                        content: _.get(res, 'data.errMesg')
                    });
                    return;
                }
                let funcode = config.appcode;
                let nodekey = '';

                if (tradeType.startsWith('2647')) {
                    nodekey = '2647_IWEB';
                } else if (tradeType.startsWith('263')) {
                    nodekey = '2631_IWEB';
                } else if (tradeType.startsWith('264a')) {
                    nodekey = '264a_WEB';
                } else if (tradeType.startsWith('261')) {
                    nodekey = '2611_IWEB';
                } else if (tradeType.startsWith('262')) {
                    nodekey = '2621_IWEB';
                } else if (tradeType.startsWith('264')) {
                    nodekey = '2641_IWEB';
                }
                print(
                    'pdf',
                    '/nccloud/erm/pub/ErmPrintBillAction.do', //后台服务url
                    {
                        funcode, //功能节点编码，即模板编码
                        nodekey, //模板节点标识
                        billtype: tradeType, //单据类型
                        oids: bills.map(item => item.billId) // 功能节点的数据主键 oids含有多个元素(['1001A41000000000A9LR','1001A410000000009JDD'])时为批量打印,
                    }
                );
            }
        });
    }

    //导出
    if (key === 'export') {
        let funCode = config.appcode;
        let appcode = config.appcode;
        let nodekey = '';
        if (tradeType.startsWith('2647')) {
            nodekey = '2647_list';
            // funcode = '201102HKSP';
        } else if (tradeType.startsWith('263')) {
            nodekey = '263X_list';
            // funcode = '201102JKSP';
        } else if (tradeType.startsWith('264a')) {
            nodekey = '264a_list';
            // funcode = '201102TZSP';
        } else if (tradeType.startsWith('261')) {
            nodekey = '261X_list';
            // funcode = '201102SQSP';
        } else if (tradeType.startsWith('262')) {
            nodekey = '262X_list';
            // funcode = '201102YTSP';
        } else if (tradeType.startsWith('264')) {
            nodekey = '264X_list';
            // funcode = '201102BXSP';
        }
        const billData = [];
        checkedRows.map(item => {
            var obj = {};
            const record = _.get(item, 'data.values', {});
            obj.billId = _.get(record, 'pk_jkbx.value', '');
            obj.billType = _.get(record, 'djlxbm.value', '');
            billData.push(obj);
        });
        const billdata = [];
        checkedRows.map(item => {
            const record = _.get(item, 'data.values', {});
            billdata.push(_.get(record, 'pk_jkbx.value', ''));
        });
        console.log(billdata);
        ajax({
            url: '/nccloud/erm/pub/ErmBatchPrintValidaAction.do',
            data: {
                billdata: billData
            },
            success: res => {
                if (!_.get(res, 'data.canPrint', false)) {
                    toast({
                        color: 'danger',
                        content: '仅支持同种交易类型单据的批量输出，请重新勾选！'
                    });
                    return;
                } else {
                    ajax({
                        url: '/nccloud/platform/print/queryTemplates.do',
                        data: {
                            funcode: funCode,
                            nodekey: nodekey,
                            appcode: appcode,
                            oids: billdata
                        },
                        success: res => {
                            if (res.data) {
                                let printTemplateID = res.data[0].m_ctemplateid;
                                let printTemplateName = res.data[0].m_vtemplatename;
                                let params = {
                                    billtype: billType,
                                    funcode: funCode,
                                    nodekey: nodekey,
                                    appcode: appcode,
                                    oids: billdata,
                                    userjson: billType,
                                    outputType: 'output',
                                    outputMode: 'excel',
                                    MultiConfig: '1',
                                    pdfConfig: false,
                                    printTemplateID: printTemplateID,
                                    htmlConfig: false,
                                    prefix: '',
                                    threshold: 0,
                                    dsPositionPolicy: 4,
                                    fileVolume: 16000,
                                    sheetNamePreFix: '新的工作表',
                                    isDifDocDifFile: false,
                                    canModifyType: false,
                                    printTemplateName: printTemplateName
                                };
                                let paramsNew = {
                                    ...params,
                                    output: true
                                };
                                console.log(paramsNew);
                                output({
                                    url: '/nccloud/erm/billmanage/BillOutPutAction.do',
                                    data: paramsNew
                                });
                            } else {
                                toast({
                                    color: 'error',
                                    content: res.data.errMesg
                                });
                            }
                        }
                    });
                }
            }
        });
    }
}
